const api = require('../../../../ws_micro_shop/utils/api.js');
const tools = require('../../../../ws_micro_shop/utils/getuserinfo.js');
const app = getApp();

let pageNum = 1;

Page({
  data: {
    history_show: false,
    search_val: '',
    list: [],
    history_info: [],
    show_loading_bar: false,
    emptyGoods: false,
    newSearch: true,
    theme: app.themeData,
    unit: '',
  },
  onLoad(opts) {
    let that = this;
    let options = opts || {};
    this.setData({
      orgPrice: app.ws_micro_shop_globalData.orgPrice,
    })
    app.tabbarindex = options.tabbarindex ? options.tabbarindex - 0 : app.tabbarindex;
    let userinfo = wx.getStorageSync('user_info');
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    Number(userinfo.info) === 0 && tools.onGetInfo(this, userinfo.auth_mode);
    app.pageOnLoad(this);
    this.setData({
      opts: options,
    });
  },
  onShow() {
    let that = this;
    wx.getStorage({
      key: 'history_info',
      success: function (res) {
        if (res.data.length > 0) {
          that.setData({
            history_info: res.data,
            history_show: true,
          });
        }
      },
    });
  },
  onReachBottom() {
    if (this.data.emptyGoods) {
      return;
    }
    if (this.data.page_count <= pageNum) {
      this.setData({
        emptyGoods: true,
      });
    }
    pageNum++;
    this.getSearchGoods();
  },
  toSearch(e) {
    let value = e.detail.value;
    if (!value) {
      return;
    }
    let history_info = this.data.history_info;
    history_info.unshift(value);
    for (let i in history_info) {
      if ({}.hasOwnProperty.call(history_info, i)) {
        if (history_info.length <= 20) {
          break;
        }
        history_info.splice(i, 1);
      }
    }
    wx.setStorageSync('history_info', history_info);
    this.setData({
      history_info,
      history_show: false,
      keyword: value,
      list: [],
    });
    this.getSearchGoods();
  },
  cancelSearchValue(e) {
    wx.navigateBack({
      delta: 1,
    });
  },
  newSearch(e) {
    let history_show = false;
    if (this.data.history_info.length > 0) {
      history_show = true;
    }
    pageNum = 1;
    this.setData({
      history_show,
      list: [],
      newSearch: [],
      emptyGoods: false,
    });
  },
  clearHistoryInfo(e) {
    let history_info = [];
    wx.setStorageSync('history_info', history_info);
    this.setData({
      history_info,
      history_show: false,
    });
  },
  getSearchGoods() {
    let that = this;
    let keyword = that.data.keyword;
    if (!keyword) {
      return;
    }
    that.setData({
      show_loading_bar: true,
    });
    wx.showLoading({
      title: '搜索中...',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.group.search,
      data: {
        keyword,
        page: pageNum,
      },
      success: function (res) {
        wx.hideLoading();
        let list;
        if (res.code === 0) {
          if (that.data.newSearch) {
            list = res.data.list;
          } else {
            list = that.data.list.concat(res.data.list);
          }
          that.setData({
            list,
            page_count: res.data.page_count,
            emptyGoods: true,
            show_loading_bar: false,
          });
          if (res.data.page_count > pageNum) {
            that.setData({
              newSearch: false,
              emptyGoods: false,
            });
          }
        }
      },
    });
  },
  historyItem(e) {
    let keyword = e.currentTarget.dataset.keyword;
    this.setData({
      keyword,
      history_show: false,
    });
    this.getSearchGoods();
  },
  collectFormId(e) {
    app.collectFormId(e);
  },
});
